package com.example.waimaiandroid.address;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;


import com.example.waimai.context.UserInfoContext;
import com.example.waimaiandroid.R;
import com.example.waimaiandroid.addAddress.addAddress;

public class AddressActivity extends AppCompatActivity {

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    // 删除成功，通知刷新
                    int position = (Integer) msg.obj;
                    // 删除成功
                    receiveAddrAdapter.userAddressList.remove(position);
                    // UserInfoContext 不用删，它们指向同一块内存
                    receiveAddrAdapter.notifyItemRemoved(position);
                    break;
                case 2:
                    // 删除失败
                    Toast.makeText(AddressActivity.this,"删除失败，请重试！",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private ImageView myBack;

    private RecyclerView recyclerView;
    private String from;

    private ReceiveAddrAdapter receiveAddrAdapter;


    private static Activity act;
    public AddressActivity(){
        act = this;
    }
    public static Activity getActivity(){
        return act;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //
        setContentView(R.layout.activity_address);
        // 设置标题栏 ，菜单栏在哪里设置的？
        Toolbar toolbar =(Toolbar)findViewById(R.id.addressToolBar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        myBack = findViewById(R.id.myAddressBack);
        recyclerView = findViewById(R.id.myReceiveAddr);

        myBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("myBack","测试一下");
                Intent intent = new Intent();
                setResult(-1,intent);
                finish();
            }
        });

        receiveAddrAdapter = new ReceiveAddrAdapter(UserInfoContext.mUserAddress,this);
        // 判断是从哪个页面跳转过来的
        Intent intent = getIntent();
        from = intent.getStringExtra("fromActivity");
        if("BillingActivity".equals(from)){
            // 如果是从支付订单页面跳转过来的
            receiveAddrAdapter.setSetSelectListner(true);
        }else if("add_update_addr".equals(from)){
            // 从新增地址页面返回的
            // 这边再通知刷新一下吧
            //receiveAddrAdapter.notifyAll();
            System.out.println(UserInfoContext.mUserAddress);
        }else{
            receiveAddrAdapter.setSetSelectListner(false);
        }
        // 设置数据
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setAdapter(receiveAddrAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if("add_update_addr".equals(from)){
            receiveAddrAdapter.notifyAll();
        }
    }

    // 新增收货地址
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_address_toolbar,menu);
        return true;
    }

    // 监听菜单栏事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // 选中了菜单栏
        // 就跳转到 新增收货地址的页面
        if(!"BillingActivity".equals(from)){
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(this, addAddress.class));
            startActivity(intent);
            finish();
        }
        return true;
    }
}